import axios from 'axios'
import { getToken } from '../utils/auth'
import { notification } from 'antd'

const service = axios.create({
  baseURL: 'http://8.131.75.136:8080/running',
  // baseURL:'http://localhost:8080/running',
  timeout: 5000,
  withCredentials: false,
})

// request interceptor
service.interceptors.request.use(
  config => {
    config.headers['token'] = getToken()
    return config
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code < 200 || res.code >= 400) {
      notification.error({
        message: '错误',
        description: res.msg
      });
      return Promise.reject('error')
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service
